var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer'], function () {
    form = layui.form();
    layer = layui.layer;
    //加载性别
    var sexTemp = $("#sexTemp").val();
    if (sexTemp != null && sexTemp != "" && sexTemp != undefined) {
        $("input[name='sex'][value=" + sexTemp + "]").attr("checked", true);
        form.render();
    }
    //提交事件
    form.on('submit(submitButton)', function () {
        //更新按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        updateUser();
    });
});

/**
 * 验证手机号唯一性
 */
function validatePhone() {
    var userId = $.trim($("#userId").val());
    // 手机
    var cellphone = $.trim($("#cellphone").val());
    var paramData = {
        "user.id": userId,
        "user.cellphone": cellphone
    };
    var urlStr = basePath + "user/validatePhone.action";
    $.ajax({
        url: urlStr,
        type: "post",
        dataType: 'text',
        data: paramData,
        success: function (data) {
            if (data != null && data != "" && data == 1) {
                layer.open({
                    title:'提示',
                    icon:0,
                    content:'手机号码已存在!',
                    yes:function (index) {
                        $("#cellphone").focus();
                        layer.close(index);
                    }
                });
                return null;
            }
        }
    });
}

function updateUser() {
    var userId = $.trim($("#userId").val());
    // 用户名
    var userName = $.trim($("#userName").val());
    // 登录名
    var loginName = $.trim($("#loginName").val());
    // 性别
    var sex = $("input[name='sex']:checked").val();
    // 年龄
    var age = $.trim($("#age").val());
    // 手机
    var cellphone = $.trim($("#cellphone").val());
    // 电话
    var telephone = $.trim($("#telephone").val());
    // 邮箱
    var email = $.trim($("#email").val());

    var type = $("#type").val();

    var paramData = {
        "user.id": userId,
        "user.userName": userName,
        "user.loginName": loginName,
        "user.cellphone": cellphone,
        "user.telephone": telephone,
        "user.sex": sex,
        "user.age": age,
        "user.email": email,
        "type": type
    };

    var urlStr = basePath + "user/saveUser.action";
    $.ajax({
        url: urlStr,
        type: "post",
        dataType: 'text',
        data: paramData,
        success: function (data) {
            if (data != null && data != "" && data == "success") {
                layer.open({
                    title: '消息',
                    icon: 1,
                    content: '个人信息更新成功!',
                    yes: function () {
                        location.reload();
                    }
                });
            } else if (data == "exist") {
                layer.open({
                    title: '提示',
                    icon: 0,
                    content: '登录名已存在!',
                    yes: function () {
                        $("#loginName").focus();
                    }
                });
            } else {
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: '个人信息修改失败!',
                    yes: function () {
                        location.reload();
                    }
                });
            }
            //更新按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });

}